# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Milan P. Stanić <mps@arvanta.net>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=zig
pkgver=0.12.0
pkgrel=0
pkgdesc="General-purpose programming language designed for robustness, optimality, and maintainability"
url="https://ziglang.org/"
# arm*, x86: fails to build (allocation failed)
# ppc64le,riscv64: builds but the test command crashes
# s390x: blocked by lld-dev
arch="x86_64 aarch64"
license="MIT"
install="$pkgname.pre-upgrade"
_llvmver=17
# will need switch to lld$_llvmver-dev later
makedepends="
	clang$_llvmver-dev
	cmake
	libxml2-dev
	lld-dev
	llvm$_llvmver-dev
	samurai
	zlib-dev
	"
provides="zig-dev=$pkgver-r$pkgrel"
subpackages="$pkgname-doc"
source="https://ziglang.org/download/$pkgver/zig-$pkgver.tar.xz
	"

build() {
	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi

	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_PREFIX_PATH=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DZIG_VERSION="$pkgver" \
		-DZIG_SHARED_LLVM=ON \
		-DZIG_TARGET_MCPU=baseline \
		-DCLANG_INCLUDE_DIRS=/usr/lib/llvm$_llvmver/include \
		$crossopts

	cmake --build build

	./build/stage3/bin/zig build docs
}

check() {
	./build/stage3/bin/zig build test \
		-Dcpu=baseline \
		-Dskip-debug \
		-Dskip-release-safe \
		-Dskip-release-small \
		-Dskip-non-native
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	mkdir -p "$pkgdir/usr/share/doc/"
	cp -r zig-out/doc "$pkgdir/usr/share/doc/$pkgname"
}

sha512sums="
0c3d9396fea4905245c7e32ce6bd9b4ff140f061cd8a957929e4c84cf105f5bbcbf2e7c030013ac649edb569e909f65d928b3e8a86f35c9076fb62e996feea14  zig-0.12.0.tar.xz
"
